package app1;

import org.apache.ibatis.session.SqlSession;

import util.MybatisUtil;

/**
 * 持久层
 * @author Alex
 *
 */
public class StudentDao {
	/**
	 * 增加学生
	 */
	public void add1() throws Exception{
		SqlSession sqlSession=null;
		try {
			 sqlSession=MybatisUtil.getSqlSession();
			 //事物开始（默认）
			 //读取StudentMapper.xml映射文件的SQL语句
			 int i=sqlSession.insert("app1.Student.add1");
			 System.out.println("本次操作影响了"+i+"行");
			 //事物提交
			 sqlSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
			//事物回滚
			sqlSession.rollback();
			throw e;
		}finally {
			MybatisUtil.closeSqlSession();
		}
	}
	/**
	 * 增加学生
	 */
	public void add2(Student student) throws Exception{
		SqlSession sqlSession=null;
		try {
			 sqlSession=MybatisUtil.getSqlSession();
			 //事物开始（默认）
			 //读取StudentMapper.xml映射文件的SQL语句
			 sqlSession.insert(Student.class.getName()+".add2",student);
			 
			 
			 //事物提交
			 sqlSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
			//事物回滚
			sqlSession.rollback();
			throw e;
		}finally {
			MybatisUtil.closeSqlSession();
		}
	}
	
	/**
	 * 测试
	 */
	
	public static void main(String[] args) throws Exception{
		/*StudentDao dao=new StudentDao();
		dao.add1();
		dao.add2(new Student(2,"李四",8000D));
		dao.add2(new Student(3,"王五",9000D));*/
	}

}
